using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VFBEW
    {
        public DataRow Convert2FBEW(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryFBEW(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["FBWMANDANT"] = " ".PadRight(2 ); 
         dwReturn["FBWJJJJ"] = " ".PadRight(0 ); 
         dwReturn["FBWBELEG"] = " ".PadRight(0 ); 
         dwReturn["FBWLFDNR"] = " ".PadRight(0 ); 
         dwReturn["FBWART"] = " ".PadRight(20 ); 
         dwReturn["FBWVAR"] = " ".PadRight(2 ); 
         dwReturn["FBWFARBE"] = " ".PadRight(10 ); 
         dwReturn["FBWGROE"] = " ".PadRight(7 ); 
         dwReturn["FBWAUFM"] = " ".PadRight(2 ); 
         dwReturn["FBWCHAR"] = " ".PadRight(7 ); 
         dwReturn["FBWGNR"] = " ".PadRight(4 ); 
         dwReturn["FBWGNRPOS"] = " ".PadRight(4 ); 
         dwReturn["FBWVPEINH"] = " ".PadRight(3 ); 
         dwReturn["FBWANZVP"] = " ".PadRight(4 ); 
         dwReturn["FBWZS"] = " ".PadRight(2 ); 
         dwReturn["FBWLO"] = " ".PadRight(2 ); 
         dwReturn["FBWLB"] = " ".PadRight(2 ); 
         dwReturn["FBWLP"] = " ".PadRight(8 ); 
         dwReturn["FBWAUFBKZ"] = " ".PadRight(1 ); 
         dwReturn["FBWAUFTYP"] = " ".PadRight(1 ); 
         dwReturn["FBWAUFKRS"] = " ".PadRight(2 ); 
         dwReturn["FBWAUFLFD"] = " ".PadRight(6 ); 
         dwReturn["FBWAUFPOS"] = " ".PadRight(3 ); 
         dwReturn["FBWAFOLGE"] = " ".PadRight(3 ); 
         dwReturn["FBWTEILNR"] = " ".PadRight(4 ); 
         dwReturn["FBWKSTKRS"] = " ".PadRight(2 ); 
         dwReturn["FBWKST"] = " ".PadRight(4 ); 
         dwReturn["FBWMNRKRS"] = " ".PadRight(2 ); 
         dwReturn["FBWMLFDNR"] = " ".PadRight(4 ); 
         dwReturn["FBWMPOS"] = " ".PadRight(2 ); 
         dwReturn["FBWSCHICHT"] = " ".PadRight(1 ); 
         dwReturn["FBWAPOS"] = " ".PadRight(4 ); 
         dwReturn["FBWSETPOS"] = " ".PadRight(4 ); 
         dwReturn["FBWBUDGET"] = " ".PadRight(8 ); 
         dwReturn["FBWFCODE"] = " ".PadRight(3 ); 
         dwReturn["FBWBS"] = " ".PadRight(2 ); 
         dwReturn["FBWWERTDAT"] = " ".PadRight(0 ); 
         dwReturn["FBWME"] = " ".PadRight(0 ); 
         dwReturn["FBWWERT"] = " ".PadRight(0 ); 
         dwReturn["FBWWEKORR"] = " ".PadRight(2 ); 
         dwReturn["FBWBUCHBEN"] = " ".PadRight(8 ); 
         dwReturn["FBWBUCHDAT"] = " ".PadRight(0 ); 
         dwReturn["FBWBUCHUHR"] = " ".PadRight(0 ); 
         dwReturn["FBWFC"] = " ".PadRight(8 ); 
         dwReturn["FBWINVBLG"] = " ".PadRight(0 ); 
         dwReturn["FBWCHARL"] = " ".PadRight(15 ); 
         dwReturn["FBWCHARLZUS"] = " ".PadRight(15 ); 
         dwReturn["FBWSTORKZ"] = " ".PadRight(1 ); 
         dwReturn["FBWKONDAT"] = " ".PadRight(0 ); 
         dwReturn["FBWMATABR"] = " ".PadRight(1 ); 
         dwReturn["FBWDISPOKZ"] = " ".PadRight(1 ); 
         dwReturn["FBWSAPKZ"] = " ".PadRight(1 ); 
         dwReturn["FBWACTION"] = " ".PadRight(2 ); 
         dwReturn["FBWFILLER"] = " ".PadRight(77 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapFBEW(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveFBEW(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryFBEW(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapFBEW(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertFBEW(dwLocal, ITXDBVERSION);
            }
            UpdateFBEW(dwLocal);
        }
        public void InsertFBEW(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO FBEW(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryFBEW(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM FBEW WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM FBEW  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "FBEW";
            Return = Result;
            return Return;
        }
        public void DeleteFBEW(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM FBEW   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateFBEW(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE FBEW SET "
                    +"FBWMANDANT= :FBWMANDANT,"
                    +"FBWJJJJ= :FBWJJJJ,"
                    +"FBWBELEG= :FBWBELEG,"
                    +"FBWLFDNR= :FBWLFDNR,"
                    +"FBWART= :FBWART,"
                    +"FBWVAR= :FBWVAR,"
                    +"FBWFARBE= :FBWFARBE,"
                    +"FBWGROE= :FBWGROE,"
                    +"FBWAUFM= :FBWAUFM,"
                    +"FBWCHAR= :FBWCHAR,"
                    +"FBWGNR= :FBWGNR,"
                    +"FBWGNRPOS= :FBWGNRPOS,"
                    +"FBWVPEINH= :FBWVPEINH,"
                    +"FBWANZVP= :FBWANZVP,"
                    +"FBWZS= :FBWZS,"
                    +"FBWLO= :FBWLO,"
                    +"FBWLB= :FBWLB,"
                    +"FBWLP= :FBWLP,"
                    +"FBWAUFBKZ= :FBWAUFBKZ,"
                    +"FBWAUFTYP= :FBWAUFTYP,"
                    +"FBWAUFKRS= :FBWAUFKRS,"
                    +"FBWAUFLFD= :FBWAUFLFD,"
                    +"FBWAUFPOS= :FBWAUFPOS,"
                    +"FBWAFOLGE= :FBWAFOLGE,"
                    +"FBWTEILNR= :FBWTEILNR,"
                    +"FBWKSTKRS= :FBWKSTKRS,"
                    +"FBWKST= :FBWKST,"
                    +"FBWMNRKRS= :FBWMNRKRS,"
                    +"FBWMLFDNR= :FBWMLFDNR,"
                    +"FBWMPOS= :FBWMPOS,"
                    +"FBWSCHICHT= :FBWSCHICHT,"
                    +"FBWAPOS= :FBWAPOS,"
                    +"FBWSETPOS= :FBWSETPOS,"
                    +"FBWBUDGET= :FBWBUDGET,"
                    +"FBWFCODE= :FBWFCODE,"
                    +"FBWBS= :FBWBS,"
                    +"FBWWERTDAT= :FBWWERTDAT,"
                    +"FBWME= :FBWME,"
                    +"FBWWERT= :FBWWERT,"
                    +"FBWWEKORR= :FBWWEKORR,"
                    +"FBWBUCHBEN= :FBWBUCHBEN,"
                    +"FBWBUCHDAT= :FBWBUCHDAT,"
                    +"FBWBUCHUHR= :FBWBUCHUHR,"
                    +"FBWFC= :FBWFC,"
                    +"FBWINVBLG= :FBWINVBLG,"
                    +"FBWCHARL= :FBWCHARL,"
                    +"FBWCHARLZUS= :FBWCHARLZUS,"
                    +"FBWSTORKZ= :FBWSTORKZ,"
                    +"FBWKONDAT= :FBWKONDAT,"
                    +"FBWMATABR= :FBWMATABR,"
                    +"FBWDISPOKZ= :FBWDISPOKZ,"
                    +"FBWSAPKZ= :FBWSAPKZ,"
                    +"FBWACTION= :FBWACTION,"
                    +"FBWFILLER= :FBWFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("FBWMANDANT", OracleType.NVarChar).Value = dwData["FBWMANDANT"].ToString(); 
          or1.Parameters.Add("FBWJJJJ", OracleType.NVarChar).Value = dwData["FBWJJJJ"].ToString(); 
          or1.Parameters.Add("FBWBELEG", OracleType.NVarChar).Value = dwData["FBWBELEG"].ToString(); 
          or1.Parameters.Add("FBWLFDNR", OracleType.NVarChar).Value = dwData["FBWLFDNR"].ToString(); 
          or1.Parameters.Add("FBWART", OracleType.NVarChar).Value = dwData["FBWART"].ToString(); 
          or1.Parameters.Add("FBWVAR", OracleType.NVarChar).Value = dwData["FBWVAR"].ToString(); 
          or1.Parameters.Add("FBWFARBE", OracleType.NVarChar).Value = dwData["FBWFARBE"].ToString(); 
          or1.Parameters.Add("FBWGROE", OracleType.NVarChar).Value = dwData["FBWGROE"].ToString(); 
          or1.Parameters.Add("FBWAUFM", OracleType.NVarChar).Value = dwData["FBWAUFM"].ToString(); 
          or1.Parameters.Add("FBWCHAR", OracleType.NVarChar).Value = dwData["FBWCHAR"].ToString(); 
          or1.Parameters.Add("FBWGNR", OracleType.NVarChar).Value = dwData["FBWGNR"].ToString(); 
          or1.Parameters.Add("FBWGNRPOS", OracleType.NVarChar).Value = dwData["FBWGNRPOS"].ToString(); 
          or1.Parameters.Add("FBWVPEINH", OracleType.NVarChar).Value = dwData["FBWVPEINH"].ToString(); 
          or1.Parameters.Add("FBWANZVP", OracleType.NVarChar).Value = dwData["FBWANZVP"].ToString(); 
          or1.Parameters.Add("FBWZS", OracleType.NVarChar).Value = dwData["FBWZS"].ToString(); 
          or1.Parameters.Add("FBWLO", OracleType.NVarChar).Value = dwData["FBWLO"].ToString(); 
          or1.Parameters.Add("FBWLB", OracleType.NVarChar).Value = dwData["FBWLB"].ToString(); 
          or1.Parameters.Add("FBWLP", OracleType.NVarChar).Value = dwData["FBWLP"].ToString(); 
          or1.Parameters.Add("FBWAUFBKZ", OracleType.NVarChar).Value = dwData["FBWAUFBKZ"].ToString(); 
          or1.Parameters.Add("FBWAUFTYP", OracleType.NVarChar).Value = dwData["FBWAUFTYP"].ToString(); 
          or1.Parameters.Add("FBWAUFKRS", OracleType.NVarChar).Value = dwData["FBWAUFKRS"].ToString(); 
          or1.Parameters.Add("FBWAUFLFD", OracleType.NVarChar).Value = dwData["FBWAUFLFD"].ToString(); 
          or1.Parameters.Add("FBWAUFPOS", OracleType.NVarChar).Value = dwData["FBWAUFPOS"].ToString(); 
          or1.Parameters.Add("FBWAFOLGE", OracleType.NVarChar).Value = dwData["FBWAFOLGE"].ToString(); 
          or1.Parameters.Add("FBWTEILNR", OracleType.NVarChar).Value = dwData["FBWTEILNR"].ToString(); 
          or1.Parameters.Add("FBWKSTKRS", OracleType.NVarChar).Value = dwData["FBWKSTKRS"].ToString(); 
          or1.Parameters.Add("FBWKST", OracleType.NVarChar).Value = dwData["FBWKST"].ToString(); 
          or1.Parameters.Add("FBWMNRKRS", OracleType.NVarChar).Value = dwData["FBWMNRKRS"].ToString(); 
          or1.Parameters.Add("FBWMLFDNR", OracleType.NVarChar).Value = dwData["FBWMLFDNR"].ToString(); 
          or1.Parameters.Add("FBWMPOS", OracleType.NVarChar).Value = dwData["FBWMPOS"].ToString(); 
          or1.Parameters.Add("FBWSCHICHT", OracleType.NVarChar).Value = dwData["FBWSCHICHT"].ToString(); 
          or1.Parameters.Add("FBWAPOS", OracleType.NVarChar).Value = dwData["FBWAPOS"].ToString(); 
          or1.Parameters.Add("FBWSETPOS", OracleType.NVarChar).Value = dwData["FBWSETPOS"].ToString(); 
          or1.Parameters.Add("FBWBUDGET", OracleType.NVarChar).Value = dwData["FBWBUDGET"].ToString(); 
          or1.Parameters.Add("FBWFCODE", OracleType.NVarChar).Value = dwData["FBWFCODE"].ToString(); 
          or1.Parameters.Add("FBWBS", OracleType.NVarChar).Value = dwData["FBWBS"].ToString(); 
          or1.Parameters.Add("FBWWERTDAT", OracleType.NVarChar).Value = dwData["FBWWERTDAT"].ToString(); 
          or1.Parameters.Add("FBWME", OracleType.NVarChar).Value = dwData["FBWME"].ToString(); 
          or1.Parameters.Add("FBWWERT", OracleType.NVarChar).Value = dwData["FBWWERT"].ToString(); 
          or1.Parameters.Add("FBWWEKORR", OracleType.NVarChar).Value = dwData["FBWWEKORR"].ToString(); 
          or1.Parameters.Add("FBWBUCHBEN", OracleType.NVarChar).Value = dwData["FBWBUCHBEN"].ToString(); 
          or1.Parameters.Add("FBWBUCHDAT", OracleType.NVarChar).Value = dwData["FBWBUCHDAT"].ToString(); 
          or1.Parameters.Add("FBWBUCHUHR", OracleType.NVarChar).Value = dwData["FBWBUCHUHR"].ToString(); 
          or1.Parameters.Add("FBWFC", OracleType.NVarChar).Value = dwData["FBWFC"].ToString(); 
          or1.Parameters.Add("FBWINVBLG", OracleType.NVarChar).Value = dwData["FBWINVBLG"].ToString(); 
          or1.Parameters.Add("FBWCHARL", OracleType.NVarChar).Value = dwData["FBWCHARL"].ToString(); 
          or1.Parameters.Add("FBWCHARLZUS", OracleType.NVarChar).Value = dwData["FBWCHARLZUS"].ToString(); 
          or1.Parameters.Add("FBWSTORKZ", OracleType.NVarChar).Value = dwData["FBWSTORKZ"].ToString(); 
          or1.Parameters.Add("FBWKONDAT", OracleType.NVarChar).Value = dwData["FBWKONDAT"].ToString(); 
          or1.Parameters.Add("FBWMATABR", OracleType.NVarChar).Value = dwData["FBWMATABR"].ToString(); 
          or1.Parameters.Add("FBWDISPOKZ", OracleType.NVarChar).Value = dwData["FBWDISPOKZ"].ToString(); 
          or1.Parameters.Add("FBWSAPKZ", OracleType.NVarChar).Value = dwData["FBWSAPKZ"].ToString(); 
          or1.Parameters.Add("FBWACTION", OracleType.NVarChar).Value = dwData["FBWACTION"].ToString(); 
          or1.Parameters.Add("FBWFILLER", OracleType.NVarChar).Value = dwData["FBWFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
